AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
local FlightSound = Sound( "PhysicsCannister.ThrusterLoop" )
local LaunchSound = Sound( "weapons/ar2/npc_ar2_altfire.wav" )

function ENT:Initialize()
	self.Entity:SetModel('models//props_c17/canister01a.mdl')

	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_NONE )

	local phys = self.Entity:GetPhysicsObject()
	
	if (phys:IsValid()) then
		phys:EnableGravity( false )
		phys:Wake()
		local PhysBone = self.Entity:GetPhysicsObjectNum( 0 )
		if ( PhysBone:IsValid() ) then
			PhysBone:SetBuoyancyRatio( 1 )
		end
	end
	self.Entity:EmitSound( LaunchSound )
	self.Entity:EmitSound( FlightSound )
	self.SpawnTime = CurTime()
	self.DeathTime = CurTime() + 10
	self.Entity:SetVelocity(self.Entity:GetUp() * 1800)

end

function ENT:PhysicsCollide( data, physobj )
	self:Explode()
end

function ENT:SetAngles( Ang )
	self.Entity:SetAngles(Ang)
end

function ENT:PhysicsUpdate( physobj )
	local Ang = self.Entity:GetUp()
	local force = Ang * 600

	physobj:ApplyForceCenter(force)
end

function ENT:Explode()
	if ( self.Exploded ) then return end
	
	self.Exploded = true
	
 	util.BlastDamage( self.Entity, self.Owner, self.Entity:GetPos(), 250, 75 )

	local effectdata = EffectData()
		effectdata:SetOrigin( self.Entity:GetPos() )
	util.Effect( "Explosion", effectdata, true, true )

end

function ENT:Think()
	if self.Solid == nil and CurTime() > (self.SpawnTime + 0.4) then
		local phys = self.Entity:GetPhysicsObject()

		phys:SetVelocity(self.Entity:GetUp() * 0)
		phys:EnableGravity( true )
		self.Entity:SetSolid( SOLID_VPHYSICS )
		self.Solid = true
	end
	
	if CurTime() > self.DeathTime then
		self:Explode()
	end
	
	if self.Exploded then
		self.Entity:Remove()
	end
end

function ENT:OnRemove()
	self.Entity:StopSound( FlightSound )
end
